package realQuestion.B;

import javafx.scene.layout.Priority;

import java.util.Arrays;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Solution2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
         int[] times=new int[n];
        for (int i = 0; i < n; i++) {
            times[i]=scanner.nextInt();
        }
        Arrays.sort(times);
        PriorityQueue<Integer> queue = new PriorityQueue<>(2);
        for (int i = 0; i < 2; i++) {
            if (queue.size()<2){
                queue.offer(times[i]);
            }else {
               int minTime= queue.poll();
               minTime+=times[i];
               queue.offer(minTime);
            }
        }
        int  maxTime=0;
        while (!queue.isEmpty()){
            int time = queue.poll();
            maxTime=Math.max(maxTime,time);

        }
        System.out.println(maxTime);

    }

}
